/**
 * 审批管理-审批列表控制器
 */
export default ['$scope','$rootScope','$location','$ui','http','$state','$timeout',function($scope,$rootScope,$location,$ui,http,$state,$timeout) {
  //url参数获取
  $scope.search = !$_fn.isEmptyObject($location.search())?$location.search():{state:1}
  console.log('$scope.search',$scope.search)
  //页面操作项分类
  $scope.state = $scope.search.state || 1
  $scope.dataList = []
  $scope.data = {order:{}}

  //警告提示
  $scope.errorToast = function(text) {
    $ui.toast({text:text,type:'warning',position:'center',appendTo:'.wp-container'});
  }
  //成功提示
  $scope.successToast = function(text) {
      $ui.toast({text:text,type:'success',position:'center',appendTo:'.wp-container'});
  }
  //获取列表数据
  let formData = {}
  formData.oid = JSON.parse($scope.search.items).oid
  formData.pid = JSON.parse($scope.search.items).pid
  http.post('selectFundPlanInit.do',formData,function (data) {
    if(data.errcode == 1){
      $scope.$apply(function(){
        let _data = data.data
        $scope.data = data.data
        $scope.contract_nobefore = data.data.order.contract_no
        //列表数据
        $scope.dataList = _data.fundPlanList
        console.log('ryy-datlist:', _data, $scope.dataList)

        if(!$scope.data.order.contract_no){
          $scope.creatNum()
        }else{
          $scope.haveCont=true
        }
      })
    }else{
      $scope.errorToast(data.errmsg || '请求失败！')
    }
  })
  //生成合同编号
  $scope.creatNum = function(){
    http.post('addContractNo.do',{},function (data) {
      if(data.errcode == 1){
        $scope.$apply(function(){
          let _data = data.data
          $scope.data.order.contract_no = data.data
          console.log('ryy-datlist:', _data)
        })
      }else{
        $scope.errorToast(data.errmsg || '请求失败！')
      }
    })
  }
  
  //提交
  $scope.sel = function(){

    let formData = {}
    formData.oid = JSON.parse($scope.search.items).oid
    formData.time = $scope.data.order.loan_time
    formData.first_party = $scope.data.order.first_party
    formData.first_party_idcard = $scope.data.order.first_party_idcard
    formData.first_party_address = $scope.data.order.first_party_address

    http.post('preservationContractNo.do',{oid:formData.oid,contract_no:$scope.data.order.contract_no},function (data) {
      if(data.errcode == 1){
        http.post('saveFundPlan.do',formData,function (data) {
          if(data.errcode == 1){
            $scope.back()
            $timeout(function(){
              $scope.successToast(data.errmsg)
            },500)
          }else{
            $scope.errorToast(data.errmsg || '请求失败！')
          }
        })
      }else{
        $scope.errorToast(data.errmsg || '请求失败！')
      }
    })


  }
  $scope.back =function(){
    window.history.back();
    window.location.reload();
  }
}]